package com.faung.configure;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ测试配置类
 * Created by Fuang on 2018-1-6.
 */
@Configuration
public class RabbitConfig {

    @Bean
    public Queue helloQueue(){
        return new Queue("hello");
    }

    // 测试topic
    @Bean
    public Queue topicAQueue(){
        return  new Queue("topicAQueue");
    }

    @Bean
    public Queue topicBQueue(){
        return  new Queue("topicBQueue");
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange", true, false);
    }

    /**
     * 符号“#”匹配一个或多个词，符号“*”匹配一个词
     * @return
     */
    @Bean
    public Binding topicABinding(Queue topicAQueue, TopicExchange topicExchange){
        return BindingBuilder.bind(topicAQueue).to(topicExchange).with("topic.#");
    }

    @Bean
    public Binding topicBBinding(Queue topicBQueue, TopicExchange topicExchange){
        return BindingBuilder.bind(topicBQueue).to(topicExchange).with("#.queue");
    }

    @Bean
    public Queue fanoutAQueue(){
        return new Queue("fanoutAQueue");
    }


    // 测试fanout
    @Bean
    public Queue fanoutBQueue(){
        return new Queue("fanoutBQueue");
    }

    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange", true, false);
    }

    @Bean
    public Binding fanoutABinding(Queue fanoutAQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutAQueue).to(fanoutExchange);
    }

    @Bean
    public Binding fanoutBBinding(Queue fanoutBQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutBQueue).to(fanoutExchange);
    }


    @Bean
    public Queue batchQueue(){
        return new Queue("batchQueue", true, false, false, null);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange", true, false);
    }

    @Bean
    public Binding batchBinding() {
        return BindingBuilder.bind(batchQueue()).to(directExchange()).with("batch-queue");
    }
}
